////////////////////////////////////////////////
// File: SysspecController.java
// Description: Enter summary here after generation.
// Date: 2016-09-23 09:09:34
// Web: http://www.elanzhou.com/
////////////////////////////////////////////////
package com.mvc.controller;
import java.io.IOException;
import java.util.List;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mvc.entity.Meminfo;
//import com.mvc.entity.User;
//import com.mvc.entity.UserDetail;
import com.mvc.entity.Sysspec;
import com.mvc.service.SyscateService;
import com.mvc.service.SysspecService;
import com.pub.MyJson;
import com.pub.Page;
import com.aspose.cells.Cells;
import com.aspose.cells.Workbook;
import com.aspose.cells.Worksheet;
@Controller
@RequestMapping("Sysspec")
public class SysspecController {
	protected final transient Log logger = LogFactory.getLog(SysspecController.class);
	@Autowired
	private SysspecService sysspecService;
	
	@Autowired
	private SyscateService syscateService;

	public SysspecController() {
	}

	/*获取品种列表*/
	@RequestMapping("getCateList.do")
	public String getCateList(HttpSession session,HttpServletRequest request,ModelMap modelMap)
	{
		String Fname=request.getParameter("Fname");
		List<Object> fcate=syscateService.getCate(Fname);
		modelMap.put("fcate", fcate);
		
		return "/Sysspec/getCateList";
	}
	
	/*列表*/
	@RequestMapping("Index.do")
	public String Index(HttpSession session,ModelMap modelMap, String pageNo, String Ajax) {

		Page page = new Page();
		page.setPageNox(pageNo);
		page.setPageSize(15);
		String hql = "select a from Sysspec a order by a.ID desc";
		List list = sysspecService.getSysspecList(hql, page);
		String counthql = "select count(*) from Sysspec a";
		page.setPageCount(sysspecService.getCount(counthql, page));
		modelMap.put("page", page);
		modelMap.put("list", list);
		if (Ajax != null && Ajax.equals("Ajax")) {
			return "/Sysspec/List";
		} else {
			return "/Sysspec/Index";
		}

	}
	/*添加*/ 
	@RequestMapping("Add.do")
	public String Add(HttpSession session,ModelMap modelMap)
	{
		Meminfo smeminfo=(Meminfo) session.getAttribute("SessionMem");
		modelMap.put("smeminfo",smeminfo);
		
		List<Object> fcate=syscateService.getFcate() ;
		modelMap.put("fcate", fcate);
		return "/Sysspec/Add";
	}
	/*保存*/
	@RequestMapping("Save.do")
	public void Save(HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap, HttpSession session) {
		Meminfo smeminfo=(Meminfo) session.getAttribute("SessionMem");
		modelMap.put("smeminfo",smeminfo);
		
		try {
			
			Sysspec  sysspec= new Sysspec();
			boolean tag=true;
		    String Msg="信息保存成功";
             
               if(tag){
                              
               String FieldFname=(String)request.getParameter("Fname");
             
               if(((FieldFname!=null)&(!FieldFname.trim().equals("")))){
            	   FieldFname=FieldFname.trim();
                    sysspec.setFname(FieldFname);
                  
        		}else
        		{
        			tag=false;
        			Msg="品类名称不能为空！";
        		}
        		}
	         
             
               if(tag){
                              
               String FieldCatename=(String)request.getParameter("Catename");
             
               if(((FieldCatename!=null)&(!FieldCatename.trim().equals("")))){
            	   FieldCatename=FieldCatename.trim();
                    sysspec.setCatename(FieldCatename);
                  
        		}else
        		{
        			tag=false;
        			Msg="品种名称不能为空！";
        		}
        		}
	         
             
               if(tag){
                              
               String FieldSpecname=(String)request.getParameter("Specname");
             
               if(((FieldSpecname!=null)&(!FieldSpecname.trim().equals("")))){
            	   FieldSpecname=FieldSpecname.trim();
                    sysspec.setSpecname(FieldSpecname);
                  
        		}else
        		{
        			tag=false;
        			Msg="规格名称不能为空！";
        		}
        		}
	         
            
            MyJson mj=new MyJson();
              if(tag){
                     sysspecService.Save(sysspec);	 
        			 mj.setStatusCode(200);
        		 }else
        		 {
        			 mj.setStatusCode(300);
        		 }
             mj.setMessage(Msg);
             mj.setForwardUrl("/Sysspec/Index.do");
             mj.setCallbackType("forward");
             String str=mj.getJson();
			 response.getWriter().print(str);
		} catch (Exception e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		}
	} 
    
	/*编辑*/
	@RequestMapping("Edit.do")
	public String Edit(HttpSession session,HttpServletRequest request,ModelMap modelMap)
	{
		Meminfo smeminfo=(Meminfo) session.getAttribute("SessionMem");
		modelMap.put("smeminfo",smeminfo);
		
		String ID=request.getParameter("ID");
		Sysspec sysspec=(Sysspec)sysspecService.getSysspec(ID);
		modelMap.put("sysspec", sysspec);
		List<Object> fcate=syscateService.getFcate() ;
		modelMap.put("fcate", fcate);
		return "/Sysspec/Edit";
	}
    
    /*编辑保存*/
	@RequestMapping("EditSave.do")
	public void EditSave(HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap,HttpSession session) {
		Meminfo smeminfo=(Meminfo) session.getAttribute("SessionMem");
		modelMap.put("smeminfo",smeminfo);
		
		try {
			String Id=(String)request.getParameter("ID");
			Sysspec  sysspec=(Sysspec)sysspecService.getSysspec(Id);
			boolean tag=true;
		    String Msg="信息修改成功";
             
            if(tag){
               
               String FieldFname=(String)request.getParameter("Fname");
             
               if(((FieldFname!=null)&(!FieldFname.trim().equals("")))){
            	   FieldFname=FieldFname.trim();
                    sysspec.setFname(FieldFname);
                  
        		}else
        		{
        			tag=false;
        			Msg="品类名称不能为空！";
        		}
        		}
	         
             
            if(tag){
               
               String FieldCatename=(String)request.getParameter("Catename");
             
               if(((FieldCatename!=null)&(!FieldCatename.trim().equals("")))){
            	   FieldCatename=FieldCatename.trim();
                    sysspec.setCatename(FieldCatename);
                  
        		}else
        		{
        			tag=false;
        			Msg="品种名称不能为空！";
        		}
        		}
	         
             
            if(tag){
               
               String FieldSpecname=(String)request.getParameter("Specname");
             
               if(((FieldSpecname!=null)&(!FieldSpecname.trim().equals("")))){
            	   FieldSpecname=FieldSpecname.trim();
                    sysspec.setSpecname(FieldSpecname);
                  
        		}else
        		{
        			tag=false;
        			Msg="规格名称不能为空！";
        		}
        		}
	         
            
            MyJson mj=new MyJson();
              if(tag){
                     sysspecService.EditSave(sysspec);	 
        			 mj.setStatusCode(200);
        		 }else
        		 {
        			 mj.setStatusCode(300);
        		 }
             mj.setMessage(Msg);
             mj.setForwardUrl("/Sysspec/Index.do");
             mj.setCallbackType("forward");
             String str=mj.getJson();
			 response.getWriter().print(str);
		} catch (Exception e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		}
	} 
    
	/*删除*/
	@RequestMapping("Del.do")
	public void Del(HttpSession session,HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap) {
		Meminfo smeminfo=(Meminfo) session.getAttribute("SessionMem");
		modelMap.put("smeminfo",smeminfo);
		
             String ID=request.getParameter("ID");
             sysspecService.Del(ID);
             String Msg="数据删除成功";
             MyJson mj=new MyJson();
             mj.setStatusCode(200);
             mj.setMessage(Msg);
             mj.setForwardUrl("/Sysspec/Index.do");
             mj.setCallbackType("forward");
             String str=mj.getJson();
             try {
                  response.getWriter().print(str);
                 } catch (IOException e) {
                 // TODO Auto-generated catch block
                 e.printStackTrace();
                 }
	      }
          	/*编辑*/
	@RequestMapping("Detail.do")
	public String Detail(HttpSession session,HttpServletRequest request,ModelMap modelMap)
	{
		Meminfo smeminfo=(Meminfo) session.getAttribute("SessionMem");
		modelMap.put("smeminfo",smeminfo);
		
		String ID=request.getParameter("ID");
		Sysspec sysspec=(Sysspec)sysspecService.getSysspec(ID);
		modelMap.put("sysspec", sysspec);
		return "/Sysspec/Detail";
	}
    
 
}
